package com.shengde.jinwawaforleader.model.myJob.adapter;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.model.myJob.classList.BindingIdCardActivity;
import com.shengde.jinwawaforleader.model.myJob.classList.ChangeStuClass;
import com.shengde.jinwawaforleader.model.myJob.classList.ChangeTeacherActivity;
import com.shengde.jinwawaforleader.model.myJob.classList.ClassDetailsActivity;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.views.ModifyTeaStuPopupWindow;

import static com.shengde.jinwawaforleader.R.id.class_name;
import static com.shengde.jinwawaforleader.R.id.number;

/**
 * Created by Chason on 2016/9/10.
 */

public class MultiTypeAdapter extends BaseAdapter {

	private static final String TAG = "MultiTypeAdapter";
	private final int VIEW_TYPE = 4;
	private final int TYPE_1 = 0;
	private final int TYPE_2 = 1;
	private final int TYPE_3 = 2;
	private final int TYPE_4 = 3;
	private ClassDetailsActivity activity;
	private LayoutInflater inflater;
	private ModifyTeaStuPopupWindow showPopupWindow;
	private String name_tea;
	private String stu_name;
	private String laoshiorxuesheng;
	private int xueshengid;
	private int laoshiid;
	private int shujuweizhi;

	public MultiTypeAdapter(ClassDetailsActivity activity) {
		this.activity = activity;
	}

	@Override
	public int getViewTypeCount() {
		return VIEW_TYPE;
	}

	@Override
	public int getItemViewType(int position) {
		if (position == 0) {
			return TYPE_1;
		} else if (position > 0 && position <= activity.tnum) {
			return TYPE_2;
		} else if (position == activity.tnum + 1) {
			return TYPE_3;
		} else {
			return TYPE_4;
		}
	}

	@Override
	public int getCount() {
		return activity.cnum + activity.tnum + 2;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		int type = getItemViewType(position);
		ViewHolder viewholder = null;
		if (convertView == null) {
			inflater = LayoutInflater.from(activity);
			switch (type) {
				case TYPE_1:
					convertView = inflater.inflate(R.layout.type3_class_details, null);
					viewholder = new ViewHolder();
					viewholder.tv_info = (TextView) convertView.findViewById(R.id.tv_info);
					convertView.setTag(viewholder);
					break;
				case TYPE_2:
					convertView = inflater.inflate(R.layout.type4_class_details, null);
					viewholder = new ViewHolder();
					viewholder.touxiang = (ImageView) convertView.findViewById(R.id.touxiang);
					viewholder.number = (TextView) convertView.findViewById(number);
					viewholder.name = (TextView) convertView.findViewById(R.id.name);
					viewholder.class_name = (TextView) convertView.findViewById(class_name);
					viewholder.change = (TextView) convertView.findViewById(R.id.change);
					convertView.setTag(viewholder);
					break;
				case TYPE_3:
					convertView = inflater.inflate(R.layout.type1_class_details, null);
					viewholder = new ViewHolder();
					viewholder.tv_info = (TextView) convertView.findViewById(R.id.tv_info);
					convertView.setTag(viewholder);
					break;
				case TYPE_4:
					convertView = inflater.inflate(R.layout.type2_class_details, null);
					viewholder = new ViewHolder();
					viewholder.touxiang = (ImageView) convertView.findViewById(R.id.touxiang);
					viewholder.number = (TextView) convertView.findViewById(number);
					viewholder.name = (TextView) convertView.findViewById(R.id.name);
					viewholder.class_name = (TextView) convertView.findViewById(class_name);
					viewholder.change = (TextView) convertView.findViewById(R.id.change);
					convertView.setTag(viewholder);
					break;
			}
		} else {
			switch (type) {
				case TYPE_1:
					viewholder = (ViewHolder) convertView.getTag();
					break;
				case TYPE_2:
					viewholder = (ViewHolder) convertView.getTag();
					break;
				case TYPE_3:
					viewholder = (ViewHolder) convertView.getTag();
					break;
				case TYPE_4:
					viewholder = (ViewHolder) convertView.getTag();
					break;
			}
		}
		switch (type) {
			case TYPE_1:
				viewholder.tv_info.setText("老师信息 " + activity.tnum + "名");
				break;
			case TYPE_2:
				Glide.with(activity).load(BaseProtocol.IMG_BASE + activity.teacherInfoEntities.get(position - 1).getLogo()).error(R.drawable.touxiang).into(viewholder.touxiang);
				viewholder.name.setText(activity.teacherInfoEntities.get(position - 1).getName());
				viewholder.number.setText(activity.teacherInfoEntities.get(position - 1).getPhone());
				String duty = "";
				if ("1".equals(activity.teacherInfoEntities.get(position - 1).getIsTopTeacher())) {
					duty = "班主任";
				} else {
					duty = "辅导老师";
				}
				viewholder.class_name.setText(activity.classname + duty);
				final String number_tea = viewholder.number.getText().toString();
				viewholder.number.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(Intent.ACTION_DIAL);
						intent.setData(Uri.parse("tel:" + number_tea));
						activity.startActivity(intent);
					}
				});
				name_tea = viewholder.name.getText().toString();
				final int teacherid = activity.teacherInfoEntities.get(position - 1).getTeacherid();
				final int ls_shujuweizhi = position - 1;
				viewholder.change.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						laoshiorxuesheng = "laoshi";
						showPopupWindow(v, teacherid, ls_shujuweizhi);
					}
				});
				break;
			case TYPE_3:
				viewholder.tv_info.setText("学生信息 " + activity.cnum + "名");
				break;
			case TYPE_4:
				Glide.with(activity).load(BaseProtocol.IMG_BASE + activity.stuInfoEntities.get(position - activity.tnum - 2).getLogo()).error(R.drawable.touxiang).into(viewholder.touxiang);
				viewholder.name.setText(activity.stuInfoEntities.get(position - activity.tnum - 2).getName());
				viewholder.number.setText(activity.stuInfoEntities.get(position - activity.tnum - 2).getGuardianContact());
				viewholder.class_name.setText(activity.classname);
				final String number_stu = viewholder.number.getText().toString();
				viewholder.number.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(Intent.ACTION_DIAL);
						intent.setData(Uri.parse("tel:" + number_stu));
						activity.startActivity(intent);
					}
				});
//				stu_name = viewholder.name.getText().toString();
				final int studentid = activity.stuInfoEntities.get(position - activity.tnum - 2).getStudentid();
				final int xs_shujuweizhi = position - activity.tnum - 2;
				viewholder.change.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						laoshiorxuesheng = "xuesheng";
						showPopupWindow(v, studentid, xs_shujuweizhi);
					}
				});
				break;
		}
		return convertView;
	}

	class ViewHolder {
		TextView tv_info;
		ImageView touxiang;
		TextView name;
		TextView number;
		TextView class_name;
		TextView change;
	}

	//显示 弹窗
	private void showPopupWindow(View v, int Id, int psition) {
		shujuweizhi = psition;
		showPopupWindow = new ModifyTeaStuPopupWindow(activity, itemsOnClick);
		if ("laoshi".equals(laoshiorxuesheng)) {
			laoshiid = Id;
			showPopupWindow.tv_video_babydiary_share.setText("更换");
		} else if ("xuesheng".equals(laoshiorxuesheng)) {
			xueshengid = Id;
			stu_name = activity.stuInfoEntities.get(psition).getName();

			showPopupWindow.tv_video_babydiary_share.setText("调班");
		}
		showPopupWindow.tv_video_babydiary_delete.setText("删除");
		//设置layout在PopupWindow中显示的位置
		showPopupWindow.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
	}

	//为弹出窗口实现监听类
	private View.OnClickListener itemsOnClick = new View.OnClickListener() {

		public void onClick(View v) {
			showPopupWindow.dismiss();
			switch (v.getId()) {
				case R.id.tv_video_babydiary_share:
					if ("laoshi".equals(laoshiorxuesheng)) {
						Intent intent = new Intent(activity, ChangeTeacherActivity.class);
						intent.putExtra("name", name_tea);
						intent.putExtra("teacherid", laoshiid);
						activity.startActivity(intent);
					} else if ("xuesheng".equals(laoshiorxuesheng)) {
						Intent intent = new Intent(activity, ChangeStuClass.class);
						Log.i(TAG, "onClick: student_name = " + stu_name);
						intent.putExtra("name", stu_name);
						intent.putExtra("studentid", xueshengid);
						intent.putExtra("curclassid", activity.classid);
						intent.putStringArrayListExtra("classlist", activity.allClass);
						intent.putIntegerArrayListExtra("allclassid", activity.allClassId);
						activity.startActivity(intent);
					}
					showPopupWindow.dismiss();
					break;
				case R.id.tv_video_babydiary_delete:
					if ("laoshi".equals(laoshiorxuesheng)) {
						BaseProtocol.getInstance().deleteTeacherInfo(activity,laoshiid + "");
					} else if ("xuesheng".equals(laoshiorxuesheng)) {
						BaseProtocol.getInstance().deleteStudentInfo(activity,xueshengid + "");
					}
					showPopupWindow.dismiss();
					break;
				case R.id.tv_video_babydiary_cancle:
					showPopupWindow.dismiss();
					break;
				case R.id.tv_modify:
					if ("laoshi".equals(laoshiorxuesheng)) {
						BaseProtocol.getInstance().getTeacherInfo(activity, laoshiid + "");
					} else if ("xuesheng".equals(laoshiorxuesheng)) {
						BaseProtocol.getInstance().getStudentInfo(activity, xueshengid + "");
					}
					showPopupWindow.dismiss();
					break;
				case R.id.tv_binding_id_card:
					Intent intent = new Intent(activity, BindingIdCardActivity.class);
					if ("laoshi".equals(laoshiorxuesheng)) {
						intent.putExtra("teacherid", laoshiid+"");
						intent.putExtra("type", "1");

					} else if ("xuesheng".equals(laoshiorxuesheng)) {
						intent.putExtra("studentid", xueshengid+"");
						intent.putExtra("type", "2");
					}
					activity.startActivity(intent);
					break;
			}
		}
	};
}